﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class TeamFutureMatches : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.TeamFutureMatches_Title;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("~/Default.aspx");
            if (Request.QueryString["ConfirmNote"] != null)
            {
                this.ConfirmNote.ForeColor = System.Drawing.Color.Green;
                this.ConfirmNote.Text = Request.QueryString["ConfirmNote"].ToString();
            }
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            
            string TeamId = null;
            if (Request.QueryString["TeamId"] != null)
                TeamId = Request.QueryString["TeamId"].ToString();
            if (TeamId == null)
            {
                TeamId = ou.TeamId;
                if ((TeamId == null))
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            }

            bool IsNational = (Request.QueryString["NationalTeam"] != null);

            if (IsNational)
                IsNationalTeamHiddenField.Value = "true";
            else
                IsNationalTeamHiddenField.Value = "false";

            if (!IsPostBack)
            {

                this.UpcomingMatchesSqlDataSource.SelectParameters.Add("TeamId", TeamId);
                this.UpcomingMatchesSqlDataSource.SelectParameters.Add("Language", ou.Language);
                this.UpcomingMatchesSqlDataSource.SelectParameters.Add("IsNT", IsNational ? "1" : "0");
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                  this.UpcomingMatchesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.ShowStadiumsValuesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.ShowStadiumsValuesSqlDataSource.SelectParameters.Add("IsNT", IsNational ? "1" : "0");
                this.StadiumsCostSQLDataSourse.SelectParameters.Add("UserId", ou.UserId);
                this.StadiumsCostSQLDataSourse.SelectParameters.Add("IsNT", IsNational ? "1" : "0");
            }
            this.MatchesLinkButton.PostBackUrl = "ListOfTeamMatches.aspx?TeamId=" + TeamId + (IsNational ? "&NationalTeam=yes" : "");
            this.TeamHomePageLinkButton.PostBackUrl = (IsNational ? "NationalTeam.aspx?Id=" : "TeamHomePage.aspx?TeamId=") + TeamId;
            this.TeamNameLinkButton.PostBackUrl = (IsNational ? "NationalTeam.aspx?Id=" : "TeamHomePage.aspx?TeamId=") + TeamId;

            this.TeamNameInTitleLinkButton.PostBackUrl = (IsNational ? "NationalTeam.aspx?Id=" : "TeamHomePage.aspx?TeamId=") + TeamId;
            this.TeamNameInTitleLinkButton.Text = GND.ReadNivdalStringData("@TeamId", "IsNational", "Nivdal_GetTeamNameByTeamId", TeamId, IsNational ? "1" : "0");

            this.TeamNameLinkButton.Text = this.TeamNameInTitleLinkButton.Text;
        }

        protected void UpcomingMatchesGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }

        protected void UpcomingMatchesGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            // If multiple buttons are used in a GridView control, use the
            // CommandName property to determine which button was clicked.
            if (e.CommandName == "AssignStadium")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = UpcomingMatchesGridView.Rows[index];
                string MatchId = row.Cells[0].Text;
                DropDownList StadiumsDropDownList = (DropDownList)row.FindControl("StadiumsDropDownList");
                string StadiumId = StadiumsDropDownList.SelectedItem.Value;
                //Response.Write(StadiumsDropDownList.SelectedItem.Value);
                if (StadiumId.ToString() == "0")
                {
                    Response.Redirect("TeamFutureMatches.aspx?ConfirmNote=" + Resources.Resource.TeamFutureMatches_ChooseStadium);
                }
                else
                    AssignTheStadium(MatchId, StadiumId);
            }



            if (e.CommandName == "UpcomingMatch")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = UpcomingMatchesGridView.Rows[index];
                string MatchId = row.Cells[0].Text;
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                
                BookMarkTheMatch(ou.UserId, MatchId);
            }

            if (e.CommandName == "BookmarkMatch")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = UpcomingMatchesGridView.Rows[index];
                string MatchId = row.Cells[0].Text;
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                BookMarkTheMatch(ou.UserId, MatchId);
            }

        }

        protected bool IsStadiumAsignment(Object StadiumAsignment)
        {
            if (StadiumAsignment.ToString() == "True")
                return true;
            else
                return false;

        }

        protected bool IsAllowBookMark()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                return true;
            else
                return false;
        }


        protected void AssignTheStadium(string MatchId, string StadiumId)
        {
            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_StadiumAssignStadiumToMatch";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_MatchId = new SqlParameter();
            dbParam_MatchId.ParameterName = "@MatchId";
            dbParam_MatchId.Value = MatchId;
            dbParam_MatchId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_MatchId);

            SqlParameter dbParam_StadiumId = new SqlParameter();
            dbParam_StadiumId.ParameterName = "@StadiumId";
            dbParam_StadiumId.Value = StadiumId;
            dbParam_StadiumId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_StadiumId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                string NT = (IsNationalTeamHiddenField.Value == "true") ? "&NationalTeam=yes" : "";
                Response.Redirect("TeamFutureMatches.aspx?ConfirmNote=" + dbParam_Answer.Value.ToString() + NT);
            }
            catch (Exception SQLDBException)
            {
                this.ConfirmNote.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }
        }


        protected void BookMarkTheMatch(string UserId, string MatchId)
        {

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_BookmarkMatch";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_BookmarkedMatch = new SqlParameter();
            dbParam_BookmarkedMatch.ParameterName = "@BookmarkedMatch";
            dbParam_BookmarkedMatch.Value = MatchId;
            dbParam_BookmarkedMatch.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_BookmarkedMatch);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("Bookmarks.aspx?ConfirmNote=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.ConfirmNote.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }

        //protected string GetIconByMatchType(object o)
        //{
        //    string ReturnValue = "~/Images/Icons/";
        //    //string Type = (string)o;
        //    int Type;
        //    try
        //    {
        //        Type = Int32.Parse(o.ToString());
        //    }
        //    catch
        //    {
        //        Type = 0;
        //    }

        //    switch (Type)
        //    {
        //        case 1:
        //            ReturnValue += "LeagueIcon.png";
        //            break;
        //        case 2:
        //            ReturnValue += "NationalCupIcon.png";
        //            break;
        //        case 3:
        //            ReturnValue += "InternationalCupIcon.png";
        //            break;
        //        default:
        //            ReturnValue += "FriendlyIcon.png";
        //            break;
        //    }
        //    return (ReturnValue);
        //}

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string AssignStadiumButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.TeamFutureMatches_AssignStadiumButtonOnClientClick + "')");
        }

        protected string BookmarkLinkButton1OnClientClick()
        {
            return ("return confirm('" + Resources.Resource.TeamFutureMatches_BookmarkLinkButton1OnClientClick + "')");
        }
    }
}